package com.lion.appletrip;

import com.lion.appletrip.fragment.Fragment1;
import com.lion.appletrip.fragment.Fragment2;
import com.lion.appletrip.fragment.Fragment3;
import com.lion.appletrip.fragment.Fragment4;
import com.lion.appletrip.fragment.Fragment5;
import com.lion.appletrip.utils.PreferenceUtils;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TabWidget;
import android.widget.Toast;

public class MainActivity extends FragmentActivity
{

	private FragmentTabHost mTabHost;
	// 获得long类型变量用于保存第一次点击事件 和第二次点击事件的时间
	private long firstTime = 0;
	private long secondTime = 0;
	private LayoutInflater inflater;
	private int[] imgs =
	{ R.drawable.tabview_a, R.drawable.tabview_b, R.drawable.tabview_c,
	        R.drawable.tabview_d, R.drawable.tabview_e };
	private View childAt;
	private boolean login;
	private View childAt2;


	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		login = PreferenceUtils.getLogin(this);
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		inflater = getLayoutInflater();
		addTab("推荐", imgs[0], Fragment1.class);
		addTab("城市猎人", imgs[1], Fragment2.class);
		addTab("添加", imgs[2], Fragment3.class);
		addTab("自由行", imgs[3], Fragment4.class);
		addTab("我的", imgs[4], Fragment5.class);
		TabWidget tabWidget = mTabHost.getTabWidget();// 获取TabHost对象
		childAt = tabWidget.getChildAt(2);
		childAt2 = tabWidget.getChildAt(4);
		
	}

	@SuppressLint("InflateParams")
    private void addTab(String title, int imageview, Class<?> clazz)
	{
		View tabWidget = inflater.inflate(R.layout.activity_main_tabview, null);
		ImageView mImageView = (ImageView) tabWidget
		        .findViewById(R.id.imageView1_tableview);
		if (imageview == imgs[2])
		{
			mImageView.setBackgroundColor(0xff4ABDCC);
		}
		mImageView.setImageResource(imageview);
		mTabHost.addTab(mTabHost.newTabSpec(title).setIndicator(tabWidget),
		        clazz, null);
	}

	/** 双击退出监听事件 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		// 获得第一次点击的时间
		firstTime = System.currentTimeMillis();
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if (firstTime - secondTime < 2000)
			{
				finish();
				return true;
			} else
			{
				secondTime = firstTime;
				Toast.makeText(this, "再次点击，退出应用", Toast.LENGTH_SHORT).show();
				return true;
			}

		}

		return super.onKeyDown(keyCode, event);

	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		// 获取意图
		Intent data = getIntent();
		int loginNum = data.getIntExtra("Login", -1);
		login = PreferenceUtils.getLogin(this);
		// 设置默认值为-1，使其一开始不进入条件判断
		if (loginNum > 0)
		{
			if (loginNum == 4)
			{
				// 显示指定片段，刷新片段登陆信息
				mTabHost.setCurrentTab(4);
			}else if (loginNum == 1)
            {
				mTabHost.setCurrentTab(1);
            }else if(loginNum == 0) {
            	mTabHost.setCurrentTab(0);
			}
		}
		if (!login)
		{
			childAt.setOnClickListener(new OnClickListener()// 监听获取后设置需要的操作
			{

				@Override
				public void onClick(View v)
				{

					Intent intent = new Intent(MainActivity.this,
					        LoginActivity.class);
					intent.putExtra("Num", 2);
					startActivity(intent);
				
				}
			});
			childAt2.setOnClickListener(new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent(MainActivity.this,
					        LoginActivity.class);
					intent.putExtra("Num", 4);
					startActivity(intent);
					
				}
			});

		}else {
			childAt.setOnClickListener(new OnClickListener()// 监听获取后设置需要的操作
			{

				@SuppressLint("InflateParams")
                @Override
				public void onClick(View v)
				{
					View view = inflater.inflate(R.layout.fragment_fragment3, null);
					PopupWindow popupWindow = new PopupWindow(view,LayoutParams.MATCH_PARENT, 
							LayoutParams.WRAP_CONTENT,true);
					ColorDrawable cd = new ColorDrawable(-0000);
					popupWindow.setBackgroundDrawable(cd);
					popupWindow.setOutsideTouchable(true);
					popupWindow.showAtLocation(childAt, Gravity.TOP, 0, 0);
					
				}
			});
		
		}
	}
}
